package eecs285.proj4.clientWin;

/** This class implements a dialog
 *  which enable users to set a 
 *  new channel.
 *  @author tywei
 */

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Vector;


public class NewChannelSettingDialog extends JDialog
{
  private JComboBox Catgs;
  private JTextField Name;
  
  private JTextArea Notice;
  private JTextArea Description;
  private JButton cancelButton;
  private JButton confirmButton;
  
  private JLabel ForName;
  private JLabel ForCats;
  private JLabel ForDescription;
  private JLabel ForNotice;
  
  private JPanel MyPanel;
  
  private JPanel TopPanel;
  private JPanel TopLeftPanel;
  private JPanel TopRightPanel;
  
  private JPanel MidPanel;
  
  private JPanel NoticePanel;
  private JPanel NoticePanelupper;
  private JPanel NoticePanellower;
  
  private JPanel DescriptionPanel;
  private JPanel DescriptionPanelupper;
  private JPanel DescriptionPanellower;
  
  private JPanel ButtonPanel;
  private Vector<String> catgs = new Vector<String>();
  
  private ActionListener buttonlistener = new Buttonlistener();
  
  public Channel newChannel = new Channel();
  public boolean confirm = false;
  static Font LetterFont = new Font( "Arial", 0, 14 );
  static Font UserFont = new Font( "Arial", 0, 18 );
  public NewChannelSettingDialog(ClientMenuFrame frame)
  {
      super(frame ,"New Channel Setting", true);
      setResizable(false);
      
      //setLayout(new BorderLayout());
      MyPanel = new JPanel(new BorderLayout());
      
      TopPanel = new JPanel(new BorderLayout());
      TopLeftPanel = new JPanel(new BorderLayout());
      ForName = new JLabel("Channel's name: ");
      ForName.setFont(LetterFont);
      Name = new JTextField(10);
      Name.setFont(UserFont);
      TopLeftPanel.add(ForName, BorderLayout.NORTH);
      TopLeftPanel.add(Name, BorderLayout.SOUTH);
      
      TopRightPanel = new JPanel(new BorderLayout());
      ForCats = new JLabel("Channel's category");
      ForCats.setFont(LetterFont);
      catgs.add("select One");
      catgs.add("arts");
      catgs.add("news");
      catgs.add("gossips");
      catgs.add("schools");
      catgs.add("sports");
      catgs.add("trade");
      catgs.add("others");
      Catgs = new JComboBox(catgs);
      Catgs.setFont(LetterFont);
      TopRightPanel.add(ForCats, BorderLayout.NORTH);
      TopRightPanel.add(Catgs, BorderLayout.SOUTH);
      
      TopPanel.add(TopLeftPanel, BorderLayout.WEST);
      TopPanel.add(TopRightPanel, BorderLayout.EAST);
      TopPanel.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
      
      NoticePanel = new JPanel(new BorderLayout());
      NoticePanelupper = new JPanel(new BorderLayout());
      ForNotice = new JLabel("Channel's notice (only seen by members)");
      ForNotice.setFont(LetterFont);
      NoticePanelupper.add(ForNotice, BorderLayout.NORTH);
      NoticePanelupper.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
      Notice = new JTextArea("", 3, 20);
      Notice.setFont(UserFont);
      NoticePanellower = new JPanel(new BorderLayout());;
      NoticePanellower.add(Notice);
      NoticePanelupper.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
      NoticePanel.add(NoticePanelupper, BorderLayout.NORTH);
      NoticePanel.add(NoticePanellower, BorderLayout.SOUTH);
      
      
      DescriptionPanel = new JPanel(new BorderLayout());
      DescriptionPanelupper = new JPanel(new BorderLayout());
      ForDescription = new JLabel("Desciptions");
      ForDescription.setFont(LetterFont);
      DescriptionPanelupper.add(ForDescription,BorderLayout.NORTH);
      DescriptionPanelupper.setBorder(BorderFactory.createEmptyBorder(10,0,5,0));
      DescriptionPanellower = new JPanel(new BorderLayout());
      Description = new JTextArea("", 3, 20);
      Description.setFont(UserFont);
      DescriptionPanellower.add(Description, BorderLayout.SOUTH);
      DescriptionPanellower.setBorder(BorderFactory.createEmptyBorder(2,0,5,0));
      DescriptionPanel.add(DescriptionPanelupper, BorderLayout.NORTH);
      DescriptionPanel.add(DescriptionPanellower, BorderLayout.SOUTH);
      
     // setBorder(BorderFactory.createRaisedBevelBorder());
      
      MidPanel = new JPanel(new BorderLayout());
      MidPanel.add(NoticePanel, BorderLayout.NORTH);
      MidPanel.add(DescriptionPanel, BorderLayout.SOUTH);
      
      ButtonPanel = new JPanel(new FlowLayout());
      cancelButton = new JButton("cancel");
      cancelButton.setFont(LetterFont);
      confirmButton = new JButton("confirm");
      confirmButton.setFont(LetterFont);
      cancelButton.addActionListener(buttonlistener);
      confirmButton.addActionListener(buttonlistener);
      ButtonPanel.add(confirmButton);
      ButtonPanel.add(cancelButton);
      
      
      MyPanel.add(TopPanel, BorderLayout.NORTH);
      MyPanel.add(MidPanel, BorderLayout.CENTER);
      MyPanel.add(ButtonPanel, BorderLayout.SOUTH);
      MyPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
      add(MyPanel);
      pack();
  }
  public class Buttonlistener implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {
          if (e.getSource().equals(cancelButton))
          {
              NewChannelSettingDialog.this.dispose();
          }
          else if (e.getSource().equals(confirmButton))
          {
              if(Name.getText().length()==0)
              {
                  JOptionPane.showMessageDialog(NewChannelSettingDialog.this,
                          "Name cannot be empty",
                          "Corrections Needed",
                          JOptionPane.WARNING_MESSAGE);
                  return;
              }
              newChannel.Modify(Name.getText(), Description.getText(), 
                      Notice.getText(), Catgs.getSelectedIndex());
              confirm=true;
              NewChannelSettingDialog.this.dispose();
          }
      }
  }
  private void Clear()
  {
      Name.setText("");
      Description.setText("");
      Notice.setText("");
      Catgs.setSelectedIndex(0);
  }
}